package com.ksd.modules.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.note.entity.AdminNoteLesson;
import com.ksd.modules.note.mapper.AdminNoteLessonMapper;
import com.ksd.modules.note.po.AdminNoteLessonSavePO;
import com.ksd.modules.note.service.IAdminNoteLessonService;
import com.ksd.modules.note.vo.AdminNoteLessonVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class AdminNoteLessonServiceImpl extends BaseServiceImpl<AdminNoteLessonMapper, AdminNoteLesson> implements IAdminNoteLessonService {

    /**
     * 查询列表
     *
     * @param noteId
     * @param chapterId
     * @return
     */
    public List<AdminNoteLessonVo> findAdminNoteLessons(Long noteId, Long chapterId) {
        LambdaQueryWrapper<AdminNoteLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteLesson::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminNoteLesson::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminNoteLesson::getNoteId, noteId);
        lambdaQueryWrapper.eq(AdminNoteLesson::getChapterId, chapterId);
        lambdaQueryWrapper.orderByAsc(AdminNoteLesson::getSorted);
        return tranferListBo(this.list(lambdaQueryWrapper), AdminNoteLessonVo.class);
    }


    /**
     * 保存和更新
     *
     * @param adminNoteLessonSavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveUpdateAdminNoteLesson(AdminNoteLessonSavePO adminNoteLessonSavePO) {
        AdminNoteLesson adminNote = tranferBo(adminNoteLessonSavePO, AdminNoteLesson.class);
        adminNote.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminNote.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return this.saveOrUpdate(adminNote);
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminNoteLessonVo getAdminNoteLessonById(Long id) {
        AdminNoteLesson adminNote = this.getById(id);
        AdminNoteLessonVo adminNoteVo = tranferBo(adminNote, AdminNoteLessonVo.class);
        return adminNoteVo;
    }


    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminNoteLessonById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, String column, Integer value) {
        boolean b = super.updateStatus(id, column, value);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id, String column, String value) {
        boolean b = super.updateStatusStr(id, column, value);
        return b;
    }

    /**
     * 根据章删除节
     * @param chapterId
     * @return
     */
    @Override
    public boolean removeByChapterId(Long chapterId){
        LambdaQueryWrapper<AdminNoteLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteLesson::getChapterId,chapterId);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据小册删除节
     * @param noteId
     * @return
     */
    @Override
    public boolean removeByNoteId(Long noteId){
        LambdaQueryWrapper<AdminNoteLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteLesson::getNoteId,noteId);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据小册删除节
     * @param noteIds
     * @return
     */
    @Override
    public boolean removeByNoteIds(List<Long> noteIds){
        LambdaQueryWrapper<AdminNoteLesson> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdminNoteLesson::getNoteId,noteIds);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminNoteLesson data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
